在 R 中, 多維資料的基礎 其原理在於高階結構並非獨立的儲存型態,而是以原子式的 向量 或 因子 加上一個 維度向量。透過使用 dim()來設定維度屬性,即可將一維序列轉換為一個 k 維陣列,將單一記憶體索引對應至多座標系統。
1. 元數據即形狀
這個 array() 函數可視為建構器,用以包裝資料(陣列、 向量或 因子)轉化為一種結構,其中 dim() 屬性決定了函數如何解析元素的組織方式。
2. 結構轉換
從一維轉換至多維,是透過賦值語法: dim(z) <- c(3,5,100)。此操作僅重新索引底層資料,而不改變其內容。
3. 初始化狀態
多維結構通常會以佔位符進行初始化: Z <- array(0, c(3,4,2)) 配置出一個 $3 \times 4 \times 2$ 的空間,將 24 個元素排列成網格。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What happens to the underlying data when you apply
dim(z) <- c(3, 5, 100)The data is multiplied by the dimension values.
R creates a copy of the data in a new format.
Only the metadata changes; the underlying vector remains the same.
The data is truncated to fit the first dimension.
✅ Correct!
Exactly. Arrays in R are just vectors with a 'dim' attribute attached.❌ Incorrect
Changing the dimension vector only alters how R interprets the data's shape, not the data itself.QUESTION 2
Which function is the primary constructor for creating a multi-dimensional structure from a single data vector?
matrix()array()dim()structure()✅ Correct!
While matrix() is for 2D, array() is the generalized constructor for n-dimensions.❌ Incorrect
array() is specifically used to wrap data into a k-way array.QUESTION 3
If you need to initialize a 3D grid of 3 rows, 4 columns, and 2 layers with zeros, which code is correct?
Z <- array(0, c(3,4,2))Z <- dim(0) <- c(3,4,2)Z <- vector(0, 3, 4, 2)Z <- array(c(3,4,2), 0)✅ Correct!
The array() function takes the data as the first argument and the dimension vector as the second.❌ Incorrect
The syntax requires array(data, dimension_vector).QUESTION 4
What is a 'k-way array' in the context of R?
An array that can only hold integers.
A specialized list of matrices.
A generalized multi-dimensional structure where 'k' is the number of dimensions.
A vector that has been sorted into k-groups.
✅ Correct!
A k-way array generalizes vectors (1D) and matrices (2D) to any number of dimensions 'k'.❌ Incorrect
The 'k' refers to the dimensionality of the structure defined by the dimension vector.QUESTION 5
Which operator is used to assign a new dimension vector to an existing object?
==<-%dim%->>✅ Correct!
In R, the assignment operator <-dim().❌ Incorrect
The standard assignment operator <-dim(x) <- valueClimate Data Restructuring
Applying Dimensionality to Large Scale Datasets
A climate scientist has a flat vector of 876,000 temperature observations (24 hours * 365 days * 100 stations). They need to reorganize this for station-by-station analysis.
Q
How would the scientist transform the flat vector 'temp_data' into a 3D structure using the dim() assignment?
Solution:
dim(temp_data) <- c(24, 365, 100)Q
What is the benefit of using a k-way array over a single long vector for this dataset?
Solution:
It allows for intuitive multi-coordinate indexing (e.g., accessing data for a specific hour on a specific day at a specific station) while R maintains the performance efficiency of a single underlying atomic vector.
It allows for intuitive multi-coordinate indexing (e.g., accessing data for a specific hour on a specific day at a specific station) while R maintains the performance efficiency of a single underlying atomic vector.